package org.jfteam.mybatis.interceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.jfteam.common.domain.*;

import java.util.ArrayList;
import java.util.Properties;

/**
 * @description: 描述
 * @author: fengwenping
 * @date: 2019-07-12 21:38
 */
@Intercepts({
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
})
public class PagingInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] queryArgs = invocation.getArgs();
        MappedStatement mappedStatement = (MappedStatement) queryArgs[0];
        //对应的sqlId
        final String sqlId = mappedStatement.getId();
        //对应的参数
        final Object param = queryArgs[1];
        //获取所有分页中的排序
        Sort sort = new Sort(new ArrayList<>());
        //构建分页参数
        Pageable pageable = new PageRequest(0, 20, sort);
        //返回分页结果
        PageImpl page = new PageImpl(new ArrayList(), pageable, 100);
        return page;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
